package com.jlnk.newdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.jlnk.newdemo.R;
import com.jlnk.newdemo.entity.NewsEntity;
import com.jlnk.newdemo.view.NewsView;

import java.util.List;

public class NewsAdapter extends ArrayAdapter<NewsEntity> {
    private Context context;

    public NewsAdapter(Context context, List<NewsEntity> newsItems) {
        super(context, 0, newsItems);
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news_title, parent, false);
        }
        NewsEntity item = getItem(position);
        TextView titleView = convertView.findViewById(R.id.news_title);
        titleView.setText(item.getTitle());
        titleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsEntity item = getItem(position);
                ((NewsView) context).showNewsDetail(item);
            }
        });
        return convertView;
    }
}